﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using HtmlAgilityPack;

namespace xuptlibsearch
{
    public partial class DelFav : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/json";
            string id = Request.Form["ID"];
            string Password = Request.Form["Password"];
            string Book = Request.Form["Book"];
            if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(Book))
            {
                Response.Write("Param_Error");
                return;
            }
            try
            {
                GetHttp(id, Password, Book);
            }
            catch
            {
                Response.Write("Server_Error");
                return;
            }
        }
        void GetHttp(string id, string password, string book)
        {
            string param = string.Format("login_type=&barcode={0}&password={1}&_=", id, password);
            byte[] PostBin = Encoding.GetEncoding("GB2312").GetBytes(param);
            CookieContainer cookie = new CookieContainer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://222.24.3.7:8080/opac_two/include/login_app.jsp");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookie;
            Stream stream = request.GetRequestStream();
            stream.Write(PostBin, 0, PostBin.Length);
            WebResponse response = request.GetResponse();
            stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GB2312"));
            string restr = reader.ReadToEnd();
            if (!restr.Contains("ok"))
            {
                Response.Write("Account_Error");
                return;
            }
            PostBin = Encoding.GetEncoding("GB2312").GetBytes("userAccount=" + id + "&recCtrlId=" + book);
            request = (HttpWebRequest)WebRequest.Create("http://222.24.3.7:8080/opac_two/virtualbookrack/Delete.action");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookie;
            stream = request.GetRequestStream();
            stream.Write(PostBin, 0, PostBin.Length);
            response = request.GetResponse();
            stream = response.GetResponseStream();
            reader = new StreamReader(stream, Encoding.GetEncoding("GB2312"));
            restr = reader.ReadToEnd();
            if (!restr.Contains(book))
            {
                Response.Write("Deleted_Successed");
                return;
            }
            else
            {
                Response.Write("Deleted_Failed");
                return;
            }
        }
    }
}